import Vue from 'vue';
import App from './App.vue';
import router from './router';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import axios from 'axios';

Vue.config.productionTip = false;
Vue.use(ElementUI);

//请求拦截器

axios.interceptors.request.use(
  config=>{
    let user = JSON.parse(localStorage.getItem("user"));
    if (user == null || user.token == null){
      return config;
    } else {
      config.headers.token = user.token;
      return config;
    }},
  error=>{
    return Promise.error(error);
});

 
// 添加一个响应拦截器
axios.interceptors.response.use(response => {
    // console.log("响应"+response)
    if(response.data.code == 500){
      router.replace({
        path:'/error'
      })
    }
    if(response.data.code == 401){
      localStorage.removeItem("user");
      // alert(response.data)
      router.replace({
        path:'/'
      })
    }
	return response;
},error => {
  // console.log("异常"+error)
  return Promise.error(error);
});

Vue.prototype.$axios = axios;

new Vue({
  router,
  render: h => h(App)
}).$mount('#app');
